﻿using System;
using System.Threading;

namespace Communication.Message
{
    // Token: 0x0200000F RID: 15
    public abstract class AbstractChannelMessage : ChannelMessage
    {
        // Token: 0x060000B1 RID: 177 RVA: 0x00002853 File Offset: 0x00000A53
        public AbstractChannelMessage(ChannelMessageType messageType) : this(1000, 500, 500, messageType)
        {
        }

        // Token: 0x060000B2 RID: 178 RVA: 0x0000286C File Offset: 0x00000A6C
        public AbstractChannelMessage(int totalRecTimeoutMillis, int spaceRecTimeoutMillis, int sendTimeoutMillis, ChannelMessageType messageType)
        {
            this.messageType = messageType;
            this.waiters = 0;
            this.totalReceiveTimeoutMillis = totalRecTimeoutMillis;
            this.spaceReceiveTimeoutMillis = spaceRecTimeoutMillis;
            this.sendTimeoutMillis = sendTimeoutMillis;
            this.isCanceled = false;
            this.isDone = false;
            this.cause = null;
            this.isRequestCancel = false;
        }

        // Token: 0x060000B3 RID: 179 RVA: 0x000028BF File Offset: 0x00000ABF
        protected override void Reset()
        {
            this.waiters = 0;
            this.isCanceled = false;
            this.isDone = false;
            this.cause = null;
            this.isRequestCancel = false;
            base.ClearCompletedHandler();
        }

        // Token: 0x060000B4 RID: 180 RVA: 0x000028EA File Offset: 0x00000AEA
        public override ChannelMessageType GetMessageType()
        {
            return this.messageType;
        }

        // Token: 0x060000B5 RID: 181 RVA: 0x000028F2 File Offset: 0x00000AF2
        public override void SetTotalReceiveTimeoutMillis(int timeoutMillis)
        {
            if (timeoutMillis < 0)
            {
                throw new ArgumentException();
            }
            this.totalReceiveTimeoutMillis = timeoutMillis;
        }

        // Token: 0x060000B6 RID: 182 RVA: 0x00002905 File Offset: 0x00000B05
        public override int GetTotalReceiveTimeoutMillis()
        {
            return this.totalReceiveTimeoutMillis;
        }

        // Token: 0x060000B7 RID: 183 RVA: 0x0000290D File Offset: 0x00000B0D
        public override void SetSpaceReceiveTimeoutMillis(int timeoutMillis)
        {
            if (timeoutMillis < 0)
            {
                throw new ArgumentException();
            }
            this.spaceReceiveTimeoutMillis = timeoutMillis;
        }

        // Token: 0x060000B8 RID: 184 RVA: 0x00002920 File Offset: 0x00000B20
        public override int GetSpaceReceiveTimeoutMillis()
        {
            return this.spaceReceiveTimeoutMillis;
        }

        // Token: 0x060000B9 RID: 185 RVA: 0x00002928 File Offset: 0x00000B28
        public override void SetSendTimeoutMillis(int timeoutMillis)
        {
            if (timeoutMillis < 0)
            {
                throw new ArgumentException();
            }
            this.sendTimeoutMillis = timeoutMillis;
        }

        // Token: 0x060000BA RID: 186 RVA: 0x0000293B File Offset: 0x00000B3B
        public override int GetSendTimeoutMillis()
        {
            return this.sendTimeoutMillis;
        }

        // Token: 0x060000BB RID: 187 RVA: 0x00002944 File Offset: 0x00000B44
        public override bool SetCancel()
        {
            lock (this)
            {
                if (this.isDone)
                {
                    return false;
                }
                this.isDone = true;
                this.isCanceled = true;
                if (this.waiters > 0)
                {
                    Monitor.PulseAll(this);
                }
            }
            base.NotifyMessageCompleted(this);
            return true;
        }

        /// <summary>
        /// 外部停止调用
        /// </summary>
        /// <returns></returns>
        public override bool Cancel()
        {
            bool result;
            lock (this)
            {
                if (this.isDone)
                {
                    result = false;
                }
                else
                {
                    this.isRequestCancel = true;
                    result = true;
                }
            }
            return result;
        }
        /// <summary>
        /// 查看当前是否已经取消
        /// </summary>
        /// <returns></returns>
        public override bool IsRequestCancel()
        {
            bool result;
            lock (this)
            {
                result = this.isRequestCancel;
            }
            return result;
        }

        // Token: 0x060000BE RID: 190 RVA: 0x00002A24 File Offset: 0x00000C24
        public override bool IsCancelled()
        {
            bool result;
            lock (this)
            {
                result = this.isCanceled;
            }
            return result;
        }

        // Token: 0x060000BF RID: 191 RVA: 0x00002A5C File Offset: 0x00000C5C
        public override bool IsDone()
        {
            bool result;
            lock (this)
            {
                result = this.isDone;
            }
            return result;
        }

        // Token: 0x060000C0 RID: 192 RVA: 0x00002A94 File Offset: 0x00000C94
        public override bool SetFailure(Exception cause)
        {
            lock (this)
            {
                if (this.isDone)
                {
                    return false;
                }
                this.isDone = true;
                this.cause = cause;
                if (this.waiters > 0)
                {
                    Monitor.PulseAll(this);
                }
            }
            base.NotifyMessageCompleted(this);
            return true;
        }

        // Token: 0x060000C1 RID: 193 RVA: 0x00002AF8 File Offset: 0x00000CF8
        public override Exception GetCause()
        {
            return this.cause;
        }

        // Token: 0x060000C2 RID: 194 RVA: 0x00002B00 File Offset: 0x00000D00
        protected bool SetSuccess()
        {
            lock (this)
            {
                if (this.isDone)
                {
                    return false;
                }
                this.isDone = true;
                if (this.waiters > 0)
                {
                    Monitor.PulseAll(this);
                }
            }
            base.NotifyMessageCompleted(this);
            return true;
        }

        // Token: 0x060000C3 RID: 195 RVA: 0x00002B5C File Offset: 0x00000D5C
        public override bool IsSuccess()
        {
            bool result;
            lock (this)
            {
                result = (this.isDone && !this.isCanceled && this.cause == null);
            }
            return result;
        }

        // Token: 0x060000C4 RID: 196 RVA: 0x00002BA8 File Offset: 0x00000DA8
        public override void AwaitUninterruptibly()
        {
            bool flag = false;
            lock (this)
            {
                while (!this.isDone)
                {
                    this.waiters++;
                    try
                    {
                        Monitor.Wait(this);
                    }
                    catch (ThreadInterruptedException)
                    {
                        flag = true;
                    }
                    finally
                    {
                        this.waiters--;
                    }
                }
            }
            if (flag)
            {
                Thread.CurrentThread.Interrupt();
            }
        }

        // Token: 0x060000C5 RID: 197 RVA: 0x00002C34 File Offset: 0x00000E34
        public override bool AwaitUninterruptibly(int timeoutMillis)
        {
            long num = (timeoutMillis <= 0) ? 0L : (DateTime.Now.Ticks / 10000L);
            long num2 = (long)timeoutMillis;
            bool flag = false;
            bool result;
            try
            {
                lock (this)
                {
                    if (this.isDone)
                    {
                        result = this.isDone;
                    }
                    else if (num2 <= 0L)
                    {
                        result = this.isDone;
                    }
                    else
                    {
                        this.waiters++;
                        try
                        {
                            for (; ; )
                            {
                                try
                                {
                                    Monitor.Wait(this, (int)num2);
                                }
                                catch (ThreadInterruptedException)
                                {
                                    flag = true;
                                }
                                if (this.isDone)
                                {
                                    break;
                                }
                                num2 = (long)timeoutMillis - (DateTime.Now.Ticks / 10000L - num);
                                if (num2 <= 0L)
                                {
                                    goto Block_11;
                                }
                            }
                            return true;
                        Block_11:
                            result = this.isDone;
                        }
                        finally
                        {
                            this.waiters--;
                        }
                    }
                }
            }
            finally
            {
                if (flag)
                {
                    Thread.CurrentThread.Interrupt();
                }
            }
            return result;
        }

        // Token: 0x04000009 RID: 9
        private readonly ChannelMessageType messageType;

        // Token: 0x0400000A RID: 10
        private int waiters;

        /// <summary>
        /// 超时时间
        /// </summary>
        private int totalReceiveTimeoutMillis;

        /// <summary>
        /// 超时时间
        /// </summary>
        private int spaceReceiveTimeoutMillis;

        private int sendTimeoutMillis;

        private bool isCanceled;

        private bool isDone;

        private Exception cause;

        /// <summary>
        /// 外部通知停止
        /// </summary>
        private bool isRequestCancel;
    }
}
